package com.kfh.ybracelet.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.base.HomeActivity;
import com.kfh.ybracelet.utils.Constant;
import com.kfh.ybracelet.utils.LogUtil;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 微信支付回调页面
 * Created by Aaron on 16/9/13.
 */
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;
//    private android.widget.TextView tvinfo;
    private TextView tv_status;
    private ImageView iv_status;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_result);

        initView();

        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID_WEIXIN);
        api.handleIntent(getIntent(), this);

//        String alipayResult = getIntent().getStringExtra("alipayResult");
//
//        if (!TextUtils.isEmpty(alipayResult)) {
//
////            alipayResult.
//            aliOrderInfo(alipayResult);
//
//        }

    }

//    /**
//     * 支付宝支付信息
//     *
//     * @param alipayResult
//     */
//    private void aliOrderInfo(String alipayResult) {
//        try {
//            JSONObject info = new JSONObject(alipayResult);
//
//            JSONObject alipay_trade_app_pay_response = info.getJSONObject("alipay_trade_app_pay_response");
//
//            String total_amount = alipay_trade_app_pay_response.getString("total_amount");//付款金额
//            String trade_no = alipay_trade_app_pay_response.getString("trade_no");//订单号
////                String out_trade_no = info.getString("out_trade_no");
//
//            tvinfo.setText(
//                    "订单号:" + trade_no +
//                            "\n\n支付金额: " + total_amount + "元");
//
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//    }

    private void initView() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        iv_left.setOnClickListener(this);
        tv_title.setText("支付结果");

        tv_status = (TextView) findViewById(R.id.tv_status);
        iv_status = (ImageView) findViewById(R.id.iv_status);

//        ImageView
//        this.tvinfo = (TextView) findViewById(R.id.tv_info);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:

                goback();

                break;

        }

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        goback();
    }

    /**
     * 返回到主页面
     */
    private void goback() {
        Intent mIntent = new Intent(this, HomeActivity.class);
        startActivity(mIntent);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

//    @Override
//    public void onReq(BaseReq req) {
//
//    }
//
//    @Override
//    public void onResp(BaseResp baseResp) {
//
//    }

    @Override
    public void onResp(BaseResp resp) {

        LogUtil.e(TAG, "errCode=:" + resp.errCode + ", resp type类型:" + resp.getType() + ", transaction : " + resp.transaction);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle(R.string.app_tip);
//            builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//            builder.show();

            switch (resp.errCode) {

                /*
                0	成功	展示成功页面
-1	错误	可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
-2	用户取消	无需处理。发生场景：用户不支付了，点击取消，返回APP。
                 */
// E/WXPayEntryActivity: errCode=-2
//            09-19 15:51:19.779 32585-32585/? E/tag:  resp type类型:5
//                case -2:
//
//                    //用户取消支付
//                    showToast("支付成功");
//
//                    break;

                case 0:

//                    showToast("支付成功");
                    iv_status.setImageResource(R.mipmap.ic_pay_success);
                    tv_status.setText("支付成功");
                    break;

                default:
//                    showToast("支付失败");
                    iv_status.setImageResource(R.mipmap.icon_slide_delete);
                    tv_status.setText("支付失败");
                    break;

            }

//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle(R.string.app_tip);
//            builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//            builder.show();
        }
//        finish();

    }

    @Override
    public void onReq(BaseReq baseReq) {

    }
}
